import { defineStore } from "pinia";
import { ref } from 'vue'
import { messageTypeListService, applicationStateListService } from "@/api/message.js";
import { roleListService } from "@/api/role";
import useUserInfoStore from "./userInfo";
import { coldStorageStateListService } from "@/api/coldStorage";
const useSystemParameter = defineStore('systemPatameter', () => {
    //定义状态相关的内容

    const roles = ref({})
    const messageTypes = ref({})
    const applicationStates = ref({})
    const coldStorageStates = ref({})

    //初始化获取系统所必须的参数集合
    const initial = async () => {
        //权限集 获取
        let params = {
            pageNum: 1,
            pageSize: 10000,
            searchString: null
        }
        let result = await roleListService(params);
        roles.value = result.data.items;
        console.log(roles.value);
        //消息类型集 获取
        result = await messageTypeListService();
        messageTypes.value = result.data;
        console.log(messageTypes.value);
        //申请状态集 获取
        result = await applicationStateListService()
        applicationStates.value = result.data;
        console.log(applicationStates.value)
        //冷库状态操作表 获取
        result = await coldStorageStateListService()
        coldStorageStates.value = result.data;
        console.log(coldStorageStates.value)
    }

    //role->roleName
    const getRoleName = (role) => {
        const roleObj = roles.value.find((r) => r.role === role);
        return roleObj ? roleObj.roleName : "RoleName not found";
    };
    //从当前用户缓存中取 -> 权限表
    const getCurrentUserRole = () => {
        const userInfoStore = useUserInfoStore()
        const roleObj = roles.value.find((r) => r.role === userInfoStore.info.role)
        return roleObj ? roleObj:"Current user Role not found";
    }
    //messageTypeId -> typeName
    const getMessageTypeName = (messageTypeId) => {
        const roleObj = messageTypes.value.find((r) => r.id === messageTypeId);
        console.log(messageTypeId);
        return roleObj ? roleObj.typeName : "messageTypeName not found";
    };
    //application 的 state -> stateName
    const getApplicationStateName = (state) => {
        const obj = applicationStates.value.find((r) => r.state === state);
        return obj ? obj.stateName : "applicationStateName not found";
    }
    //coldStorg 的 state -> stateName
    const getColdStorageStateName = (state) => {
        const obj = coldStorageStates.value.find((r) => r.state === state);
        return obj ? obj.stateName : "applicationStateName not found";
    }
    //冷库状态 -> 切换操作表
    const getColdStorageOperation = (coldStorageState) => {
        const obj = coldStorageStates.value.find((r) => r.state === coldStorageState)
        console.log(obj);
        return obj?obj:"coldStorageStatesOpetation not found"
    }

    return { roles, messageTypes, applicationStates, coldStorageStates, initial, getRoleName, getMessageTypeName, getCurrentUserRole, getApplicationStateName, getColdStorageStateName, getColdStorageOperation }
}, { persist: true })

export default useSystemParameter;


